from typing import Optional, List, Dict, Any
from datetime import datetime

from pydantic import BaseModel, Field


# 共享属性
class ProjectBase(BaseModel):
    name: str
    code: str
    description: Optional[str] = None
    status: str = "planning"
    start_date: datetime
    planned_end_date: datetime
    budget: Optional[float] = None
    priority: int = 0


# 创建项目时需要的属性
class ProjectCreate(ProjectBase):
    parent_id: Optional[int] = None
    manager_id: int


# 更新项目时可以更新的属性
class ProjectUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    status: Optional[str] = None
    start_date: Optional[datetime] = None
    planned_end_date: Optional[datetime] = None
    actual_end_date: Optional[datetime] = None
    budget: Optional[float] = None
    actual_cost: Optional[float] = None
    progress: Optional[float] = None
    priority: Optional[int] = None
    manager_id: Optional[int] = None


# 阶段基础模型
class PhaseBase(BaseModel):
    name: str
    description: Optional[str] = None
    status: str = "not_started"
    start_date: datetime
    end_date: datetime
    order: int = 0


# 创建阶段
class PhaseCreate(PhaseBase):
    project_id: int


# 更新阶段
class PhaseUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    status: Optional[str] = None
    start_date: Optional[datetime] = None
    end_date: Optional[datetime] = None
    order: Optional[int] = None


# 里程碑基础模型
class MilestoneBase(BaseModel):
    name: str
    description: Optional[str] = None
    due_date: datetime
    is_completed: bool = False


# 创建里程碑
class MilestoneCreate(MilestoneBase):
    project_id: int
    phase_id: Optional[int] = None


# 更新里程碑
class MilestoneUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    due_date: Optional[datetime] = None
    completed_date: Optional[datetime] = None
    is_completed: Optional[bool] = None
    phase_id: Optional[int] = None


# 任务基础模型
class TaskBase(BaseModel):
    name: str
    description: Optional[str] = None
    status: str = "not_started"
    priority: str = "medium"
    start_date: datetime
    due_date: datetime
    estimated_hours: Optional[float] = None


# 创建任务
class TaskCreate(TaskBase):
    project_id: int
    phase_id: Optional[int] = None
    milestone_id: Optional[int] = None
    parent_id: Optional[int] = None
    assignee_id: Optional[int] = None


# 更新任务
class TaskUpdate(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    status: Optional[str] = None
    priority: Optional[str] = None
    start_date: Optional[datetime] = None
    due_date: Optional[datetime] = None
    completed_date: Optional[datetime] = None
    estimated_hours: Optional[float] = None
    actual_hours: Optional[float] = None
    progress: Optional[float] = None
    phase_id: Optional[int] = None
    milestone_id: Optional[int] = None
    parent_id: Optional[int] = None
    assignee_id: Optional[int] = None


# 任务依赖关系
class TaskDependencyCreate(BaseModel):
    task_id: int
    dependency_id: int
    dependency_type: str = "finish_to_start"
    lag_days: int = 0


# 任务评论
class TaskCommentCreate(BaseModel):
    task_id: int
    content: str


# API响应中的项目信息
class ProjectResponse(ProjectBase):
    id: int
    parent_id: Optional[int] = None
    manager_id: int
    creator_id: int
    actual_end_date: Optional[datetime] = None
    actual_cost: Optional[float] = None
    progress: float = 0.0
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# API响应中的阶段信息
class PhaseResponse(PhaseBase):
    id: int
    project_id: int
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# API响应中的里程碑信息
class MilestoneResponse(MilestoneBase):
    id: int
    project_id: int
    phase_id: Optional[int] = None
    completed_date: Optional[datetime] = None
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# API响应中的任务信息
class TaskResponse(TaskBase):
    id: int
    project_id: int
    phase_id: Optional[int] = None
    milestone_id: Optional[int] = None
    parent_id: Optional[int] = None
    assignee_id: Optional[int] = None
    creator_id: int
    completed_date: Optional[datetime] = None
    actual_hours: Optional[float] = None
    progress: float = 0.0
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# API响应中的任务依赖关系信息
class TaskDependencyResponse(BaseModel):
    task_id: int
    dependency_id: int
    dependency_type: str
    lag_days: int

    class Config:
        from_attributes = True


# API响应中的任务评论信息
class TaskCommentResponse(BaseModel):
    id: int
    task_id: int
    user_id: int
    content: str
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True